import java.util.Date;
import org.joda.time.*

import edu.uow.devdocs.*
import edu.uow.devdocs.System;

class BootStrap {

    def init = { servletContext ->
		def i1 = new IssueStatus(status:'New',activeFlag:true).save()
		new IssueStatus(status:'Closed',activeFlag:false).save()
		new IssueStatus(status:'Open',activeFlag:true).save()
		new IssueStatus(status:'Deferred',activeFlag:false).save()
		new IssueStatus(status:'Awaiting Development',activeFlag:true).save()
		
		new DowntimeReportStatus(status:'Open',activeFlag:true).save()
		new DowntimeReportStatus(status:'Closed',activeFlag:false).save()
		
		def d1 = new DevelopmentTaskStatus(status:'New',activeFlag:true).save()
		new DevelopmentTaskStatus(status:'Closed',activeFlag:false).save()
		new DevelopmentTaskStatus(status:'Open',activeFlag:true).save()
		new DevelopmentTaskStatus(status:'Deferred',activeFlag:false).save()
		new DevelopmentTaskStatus(status:'Development Complete',activeFlag:true).save()

		def dcs1 = new DevelopmentChecklistStatus(status:'New',activeFlag:true).save()
		new DevelopmentChecklistStatus(status:'Closed',activeFlag:false).save()
		new DevelopmentChecklistStatus(status:'Open',activeFlag:true).save()
		new DevelopmentChecklistStatus(status:'Deferred',activeFlag:false).save()
		
		new DeploymentStatus(status:'Cancelled',activeFlag:false).save()
		def deploymentStatus1 = new DeploymentStatus(status:'Planned',activeFlag:true).save()
		printValidationErrors(deploymentStatus1)
		new DeploymentStatus(status:'Deployed',activeFlag:false).save()
		
		//new TestIssueStatus(status:'New',activeFlag:true).save()
		//new TestIssueStatus(status:'Fixed in Next Build',activeFlag:true).save()
		//new TestIssueStatus(status:'User Error',activeFlag:true).save()
		//new TestIssueStatus(status:'Requirements Implementation Error',activeFlag:true).save()
		//new TestIssueStatus(status:'Deferred to Next Release',activeFlag:false).save()
		
		new AssignmentStatus(status:'New',activeFlag:true).save()
		new AssignmentStatus(status:'Open',activeFlag:true).save()
		new AssignmentStatus(status:'Closed',activeFlag:false).save()
		
		new ProjectStatus(status:'Completed',activeFlag:false).save()
		new ProjectStatus(status:'Active Operation Project',activeFlag:true).save()
		def projectStatus1 = new ProjectStatus(status:'Active ITPAC Project',activeFlag:true).save()
		new ProjectStatus(status:'Active ITS Admin Project',activeFlag:true).save()
		new ProjectStatus(status:'New',activeFlag:true).save()
		new ProjectStatus(status:'Active Support Project',activeFlag:true).save()
		
		def issueType1 = new IssueType(name:'Bug').save()
		new IssueType(name:'Business Requirement').save()
		new IssueType(name:'Support Request').save()
		new IssueType(name:'Enhancement').save()
		
		def phase1 = new Phase(description:'Phase1',activeFlag:true, displayOrder: 1).save()
		def phase2 = new Phase(description:'Phase2',activeFlag:true, displayOrder: 3).save()
		def phase3 = new Phase(description:'Phase3',activeFlag:true, displayOrder: 2).save()
		
		def projectType1 = new ProjectType(projectType: 'ProjectType').save()
		
		def team1 = new Team(team: 'Special Projects').save()
		def person1 = new Person(username: 'person1', team: team1, landingPage: 'TimeTracker').save()
		person1.manager = person1
		person1.save()
		
		def person2 = new Person(username: 'person2', team: team1, landingPage: 'TimeTracker').save()
		person2.manager = person1
		person2.save()
		
		def person3 = new Person(username: 'person3', team: team1, landingPage: 'TimeTracker').save()
		person3.manager = person1
		person3.save()
		
		def secUser1 = new SecUser(username:'person1','password':'person1','enabled':true).save(flush:true)
		def secUser2 = new SecUser(username:'person2','password':'person1','enabled':true).save(flush:true)
		def secUser3 = new SecUser(username:'person3','password':'person1','enabled':true).save(flush:true)
		
		def roleSystemAdmin = new SecRole(authority:'ROLE_SYSTEM_ADMIN').save(flush:true)
		def roleProjectAdmin = new SecRole(authority:'ROLE_PROJECT_ADMIN').save(flush:true)
		def rolePersonAdmin = new SecRole(authority:'ROLE_PERSON_ADMIN').save(flush:true)
		def rolePhaseAdmin = new SecRole(authority:'ROLE_PHASE_ADMIN').save(flush:true)
		def roleChecklistAdmin = new SecRole(authority:'ROLE_CHECKLIST_ADMIN').save(flush:true)
		def rolUserAdmin = new SecRole(authority:'ROLE_USER_ADMIN').save(flush:true)

		SecUserSecRole.create secUser2, roleSystemAdmin, true
		SecUserSecRole.create secUser2, roleProjectAdmin, true
		SecUserSecRole.create secUser2, rolePersonAdmin, true
		SecUserSecRole.create secUser2, rolePhaseAdmin, true
		SecUserSecRole.create secUser2, roleChecklistAdmin, true
		SecUserSecRole.create secUser2, rolUserAdmin, true

		def system1 = new System(name:'System1').save()
		
		new System(name:'system2').save()
		def project1 = new Project(system:system1, 
			name:'Project1', 
			status: projectStatus1, 
			estimatedHours: 40, 
			description: 'description', 
			availableToAllFlag: false, 
			projectType: projectType1,
			costCentre: '12345',
			flagBillable: true,
			businessOwner: person2,
			developmentManager:  person1).save()
			
		(5..50).each{
			new Project(system:system1,
				name:'Project' + it,
				status: projectStatus1,
				estimatedHours: 40,
				description: 'description',
				availableToAllFlag: false,
				projectType: projectType1,
				costCentre: '12345',
				flagBillable: true,
				businessOwner: person2,
				developmentManager:  person1).save()
		}	
		def project2 = new Project(system:system1,
			name:'Project2',
			status: projectStatus1,
			estimatedHours: 40,
			description: 'description',
			availableToAllFlag: false,
			projectType: projectType1,
			costCentre: '12345',
			flagBillable: true,
			businessOwner: person1,
			developmentManager:  person1).save()
		
		def project3 = new Project(system:system1,
			name:'Project3',
			status: projectStatus1,
			estimatedHours: 40,
			description: 'description',
			availableToAllFlag: false,
			projectType: projectType1,
			costCentre: '12345',
			flagBillable: true,
			businessOwner: person1,
			developmentManager:  person1).save()
			
		project1.addToPersons(person1)
		project1.addToPersons(person2)
		project1.addToPersons(person3)
		
		printValidationErrors(project1)
		project1.save(flush:true)
		
		printValidationErrors(person2)

		
		def issue1 = new Issue(description:'Issue1 description',
			priority:3,
			issue: 'Issue1',
			dateCreated:new Date(),
			estimatedHours:40,
			referenceNumber: '111111',
			project:project1,
			status:i1,
			person:person1,
			issueType:issueType1)
		
		def comment = new CommentHistory(person:person1,comment:'comment1',issue:issue1)
		comment.save()
		

		issue1.save()

		def deployment1 = new Deployment(deploymentVersion:'2.4',
			dateOfPlannedDeployment:new Date(),
			dateOfPlannedTestDeploy: new Date(),
			project:project1,
			status:deploymentStatus1)
		

		
		printValidationErrors(deployment1)
		
		deployment1.save(flush:true)
		//def timeTracker1 = new TimeTracker()
				
		def devTask1 = new DevelopmentTask(
			dateCreated: new Date(),
			task:'Development task 1',
			priority:3,
			reference:'12233',
			dateRequired: new Date(),
			project: project1,
			status:d1,
			issue: issue1)
		//devTask1.save()
		devTask1.errors.allErrors.each {
			println it
		}
		
		def devTask2 = new DevelopmentTask(
			dateCreated: new Date(),
			task:'Development task 2',
			priority:2,
			reference:'12333',
			dateRequired: new Date(),
			project: project1,
			status:d1,
			issue: issue1)
		//devTask2.save()
		devTask2.errors.allErrors.each {
			println it
		}
		
		issue1.addToDevelopmentTasks(devTask1)
		issue1.addToDevelopmentTasks(devTask2)
		
		//issue1.developmentTasks.add(devTask1).save()
		issue1.save();
		
		def chk1 = new Checklist(description:'Deployment Plan',
			checklist:'this is a test',
			renderHtmlFlag: false).save()

		def chk2 = new Checklist(description:'Unit Test',
			checklist:'<h5>heading</h5>this <b>is</b> an html test',
			renderHtmlFlag: true).save()
		
		def devChkList1 = new DevelopmentChecklist(description:'Description 1',
			project:project1,
			status:dcs1,
			checklist:chk1,
			checklistContent:chk1.checklist
			)
		
		def devChkList2 = new DevelopmentChecklist(description:'Description 2',
			project:project1,
			status:dcs1,
			checklist:chk2,
			checklistContent:chk1.checklist
			)
		
		devTask1.addToDevelopmentChecklists(devChkList1).save()
		devTask1.addToDevelopmentChecklists(devChkList2).save()
		
		deployment1.addToDevelopmentTasks(devTask1);
		deployment1.addToDevelopmentTasks(devTask2);
		deployment1.save()
		
		def dt = new DateTime()
		(1..50).each{tm->
			
			def tt1 = new TimeTracker(dateOfWork:new Date(dt.millis),comments:'comment'+tm,project:project1,person:person1,phase:phase1, hours:3)
			printValidationErrors(tt1)
			tt1.save()
			new TimeTracker(dateOfWork:new Date(dt.millis),comments:'comment'+tm,project:project1,person:person1,phase:phase2, hours:3).save()
			new TimeTracker(dateOfWork:new Date(dt.millis),comments:'comment'+tm,project:project1,person:person1,phase:phase3, hours:1).save()
			dt = dt.minusDays(tm)
		}

		

    }
    def destroy = {
    }
	
	def printValidationErrors(obj) {
		obj.validate()
		obj.errors.allErrors.each {
			println it
		}
	}
}
